const express = require("express");
const bodyParser = require("body-parser");
const cors = require("cors");
const fs = require("fs");
const { cleanHtml } = require("./easyclean");

const app = express();
const port = 55890;

app.use(bodyParser.json());
app.use(cors()); // 使用 cors 中间件

app.post("/clean", (req, res) => {
  const { html } = req.body;
  if (!html) {
    return res.status(400).send("HTML 内容是必需的");
  }

  const cleanedHtml = cleanHtml(html);

  // 输出到新文件或打印到控制台
  fs.writeFileSync("cleanedFile.html", cleanedHtml, "utf8");

  const successMessage = "HTML 内容已清理并保存到 cleanedFile.html 文件中";

  res.status(200).json({ message: successMessage });
});

app.listen(port, () => {
  console.log(`服务器正在运行在 http://localhost:${port}`);
});
